from time import sleep

from General.Mapper import *
from DataProcess.BaiDuNLP import *


class DataProcesser:
    nlp = NLP()

    # 以下权重按照(1-0.93↑(20-X))*100的公式生成
    weight: map = {1: 426, 2: 397, 3: 369, 4: 343, 5: 319, 6: 296, 7: 276, 8: 256, 9: 238, 10: 222, 11: 206,
                   12: 192, 13: 178, 14: 166, 15: 154, 16: 143, 17: 133, 18: 124, 19: 115, 20: 107}

    def __init__(self):
        """
            初始化函数
        """
        pass


    def countTag(self, rawDataList: list, topicList: list) -> dict:
        """
        统计给定的原始数据tag
        :param rawDataList: 传入需统计的原始数据
        :param rawDataList: 传入所有话题列表
        :return: map 键为tagId, 值为统计次数 的map
        """
        tagCount: dict = {}
        topicMap: dict = {}

        for item in topicList:  # 构造根据topicId映射到topic的map
            topicMap[item.id] = item
        for item in rawDataList:
            if item.topicId in topicMap:  # 未加载的topic则自动跳过
                if topicMap[item.topicId].tagId in tagCount:
                    tagCount[topicMap[item.topicId].tagId] = tagCount[topicMap[item.topicId].tagId] + 1
                else:
                    tagCount[topicMap[item.topicId].tagId] = 1
        return tagCount

    def countHotestTopic(self, rawData: list) -> dict:
        """
        按照给定原始数据，依照权重分配表统计对应加权热度
        :param rawData: 传入需统计的原始数据
        :return: map 键为topicId, 值为热度权重的map
        """
        topicCount: dict = {}

        for item in rawData:
            if item.topicId in topicCount:
                topicCount[item.topicId] = topicCount[item.topicId] + self.weight[item.ranking]
            else:
                topicCount[item.topicId] = self.weight[item.ranking]
        return topicCount

    def parseWord(self, topicList: list) -> dict:
        """
        通过话题列表解析话题中的名词
        :param topicList: 话题列表
        :return: 返还dict，key为话题id，键为名词集合
        """
        resultMap: dict = {}
        for item in topicList:
            # 用于控制解析速度不超过接口限制2QPS
            sleep(1)
            resultMap[item.id] = self.nlp.parse(item.topicName)
        return resultMap
